<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
class WdkDbo
{
	private	$link,
	$host,
	$port,
	$userid,
	$password,
	$database,
	$debug,
	$result,
	$queries;
	
	function __construct()
	{
		$this->queries = array();
		$this->loadConfig();
		$this->connect();
	}
	
	private function loadConfig()
	{
		$config = Wdk::config();
		foreach($config['db'] as $k=>$v)
		{
			$this->{$k} = $v;
		}
	}
	
	private function connect()
	{
		$this->link	= mysql_connect($this->host.':'.((int) $this->port > 0 ? $this->port : 3306), $this->userid, $this->password);
		if(!$this->link)
		{
			throw new DatabaseConnectionError("Can not connect to database server : ". $this->error());
		}
		if(!mysql_select_db($this->database, $this->link))
		{
			throw new DatabaseConnectionError("Can not use selected database [{$this->database}] : ". $this->error());
		}
		$this->execute("set names utf8;");
	}
	
	public function execute($sql)
	{
		$this->queries[] = $sql;
		$this->result = mysql_query($sql, $this->link);
		if (!$this->result)
		{
			throw new DatabaseSqlException("[".$this->errorNo()."]".$this->error() .' - '. $sql);
		}
	}
	
	public function rows()
	{
		$rows = array();
		while($row = mysql_fetch_assoc($this->result))
		{
			$rows[] = $row;
		}
		return $rows;
	}
	
	public function row()
	{
		$row = mysql_fetch_assoc($this->result);
		return $row;
	}
	
	public function numRows()
	{
		return mysql_num_rows($this->result);
	}
	
	final public function insertSql($table='', $data=array())
	{
		$table = (string) $table;
		$data = (array) $data;
		
		$values = array();
		$fields = array();
		foreach($data as $k=>$v)
		{
			$fields[] = $k;
			$values[] = is_int($v) ? $v : "'".$this->escape($v)."'";
// 			$values[] = is_int($v) ? $v : "'{$v}'";
		}
		
		return sprintf(	"INSERT INTO %s (%s) VALUES (%s)"
						, $table
						, implode(",", $fields)
						, implode(",", $values)
					);
	}
	
	final public function updateSql($table='', $data=array(), $condition='')
	{
		$table = (string) $table;
		$data = (array) $data;
		$condition = (string) $condition;
		
		$fields = array();
		foreach($data as $k=>$v)
		{
			$fields[] = "{$k} = ".(is_int($v) ? $v : "'".$this->escape($v)."'");
// 			$fields[] = "{$k} = ".(is_int($v) ? $v : "'{$v}'");
		}
		return sprintf(	"UPDATE %s SET %s %s"
						, $table
						, implode(",", $fields)
						, ($condition == '') ? '' : 'WHERE '.$condition
					);
	}
	/******************************************************************************************/
	final public function allQuery()
	{
		return $this->queries;
	}
	final public function lastQuery()
	{
		return $this->queries[count($this->queries)];
	}
	final public function insertId()
	{
		return mysql_insert_id($this->link);
	}
	final public function affectedRows()
	{
		return mysql_affected_rows($this->link);
	}
	final public function errorNo()
	{
		return mysql_errno($this->link);
	}
	final public function error()
	{
		return mysql_error();
	}
	final public function escape($str)
	{
		return is_int($str) ? $str : mysql_real_escape_string($str);
	}
	/******************************************************************************************/
	final public function transStart()		{}
	final public function transComplete()	{}
	final public function transOk()			{}
}